<?php
/**
 * AuthenticationController for EIcms
 * 
 * @author hcderaad
 * @package eicms
 * @subpackage controllers
 */
class AuthController extends EICms_Controller_Action {
	
	/**
	 * Sets the texts for the View object.
	 *
	 * @return void
	 */
	public function setViewTexts() {
		$this->view->title = $this->_('authentication');
		$this->view->content = $this->_('providecredentials') . ":";
	}
	
	/**
	 * Shows the index action with the loginform
	 *
	 */
	public function indexAction() {
		$this->setViewTexts();
		$form = new EICms_Form_Login();
		$this->view->loginForm = $form;
	}

	/**
	 * Attempts to perform a login.
	 *
	 */
	public function loginAction() {
		$form = new EICms_Form_Login();
		if(!$form->isValid($this->getRequest()->getParams())) {
			$this->setViewTexts();
			$this->view->loginForm = $form;
		}
		else {
			$vals = $form->getValues();
			$result = EICms_Auth::getInstance()->login($vals['name'],$vals['password']);
			if(!$result->isValid()) {
				$this->view->loginForm = $form;
			}
			else {
				$this->_forward('index','index');
			}
		}
	}
	
	/**
	 * Logs out the currently logged in user.
	 *
	 */
	public function logoutAction() {
		EICms_Auth::getInstance()->logout();
		$this->_forward('index','index');
	}
	
	/**
	 * Displays the authenticated users information.
	 *
	 */
	public function infoAction() {
		$this->view->title = $this->_('info');
		$identityArray = (array) EICms_Auth::getInstance()->getIdentity();
		unset($identityArray['id']);
		$this->view->identity = array();
		foreach($identityArray as $key => $val) {
			$this->view->identity[] = array(ucwords(str_replace('_',' ',$key)),ucwords($val));
		}
	}
}
?>